<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数组案例</title>
    <script>
        //求数组之和,平均值
        /*         var arr = [2, 6, 1, 7, 4];
                var sum = 0;
                for (var i = 0; i < arr.length; i++) {
                    sum = sum + arr[i];
                }
                var avr = sum / arr.length;
                console.log('和为' + sum + '  平均值为' + avr); */

        //数组最大值
        /*         var arr = [2, 6, 1, 77, 52, 25, 7];
                var max = arr[0];
                for (var i = 0; i < arr.length; i++) {
                    if (arr[i] > max)
                        max = arr[i];
                }
                console.log('最大值为:' + max); */

        //数组转换为分割字符串
        /*         var arr = ['red', 'green', 'blue', 'pink'];
                var str = '';
                for (var i = 0; i < arr.length; i++) {
                    str += arr[i] + '|';
                }
                console.log(str); */

        //新增数组存放整数
        /*         var arr = [];
                arr.length = prompt('你要存放数的个数为:');
                for (var i = 0; i < arr.length; i++) {
                    arr[i] = prompt('请输入第' + (i + 1) + '个数')
                }
                alert(arr); */

        //筛选数组 >=10的提出
        /*         var arr = [2, 0, 6, 1, 77, 0, 52, 0, 25, 79];
                var newArr = [];
                for (var i = 0; i < arr.length; i++) {
                    if (arr[i] >= 10) {
                        newArr[newArr.length] = arr[i];
                    }
                }
                console.log(newArr.length);
                console.log(newArr); */

        //数组删除指定元素0
        /*          var arr = [2, 0, 6, 1, 77, 0, 52, 0, 25, 7];
                var newArr = [];
                for (var i = 0; i < arr.length; i++) {
                    if (arr[i] != 0)
                    newArr[newArr.length] = arr [i];
                }
                console.log(newArr); */

        //数组去重
        //方法1
/*                 var arr = [2, 0, 6, 1, 77, 0, 52, 0, 25, 7];
                var newArr = [];
                for (var i = 0; i < arr.length; i++) {
                    if (newArr.indexOf(arr[i]) == -1)
                        newArr.push(arr[i]);
                }
                console.log(newArr); */
        //方法2
/*         var arr = [0, 4, 1, 1, 1, 1, 1, 2, 3, 3, 4, 5, 5, 5, 6];
        for (let i = 0; i < arr.length; i++){
            for (let j = i + 1; j < arr.length; j++)
            {
                if (arr[i] == arr[j]){
                    arr.splice(j, 1);
                    i = -1;
                }
            }
        }
        console.log(arr); */

        //数组翻转
        /*         var arr = ['red', 'green', 'blue', 'pink', 'purple'];
                var newArr = [];
                for (var i = arr.length - 1; i >= 0; i--) {
                    newArr[newArr.length] =  arr[i];
                }
                console.log(newArr); */

        //冒泡排序
        /*         var arr = [5, 3, 4, 2, 1];
                var temp;
                for (var i = 0; i < arr.length; i++) {
                    for (var j = 0; j < arr.length - i; j++) {
                        if (arr[j + 1] < arr[j]) {
                            temp = arr[j];
                            arr[j] = arr[j + 1];
                            arr[j + 1] = temp;
                        }
                    }
                }
                console.log(arr); */


                //数组常用方法练习
/*                 var arr = [];
                var push = arr.push("123", "456");
                var pop = arr.pop();
                var unshift = arr.unshift("789");
        
                for (let i = 0; i < unshift; i++)
                    var shift = arr.shift();
        
                var Person = function (name, age) {
                    this.name = name;
                    this.age = age;
                };
                Person.prototype.toString = function () {
                    return "I'm " + this.name + "," + this.age + " years old";
                }
        
                var per1 = new Person("yi", 1);
                var per2 = new Person("shiba", 18);
                var per3 = new Person("ershi", 20);
        
                arr.push(per1, per2, per3, 1, 2, 5);
        
                function judgeAge(arr) {
                    let arrNew = [];
                    for (let i = 0; i < arr.length; i++) {
                        if (arr[i].age >= 18)
                            arrNew.push(arr[i]);
                    }
                    return arrNew;
                }
        
                var arrNew = judgeAge(arr);
        
                for (k in arrNew){
                    console.log(arrNew[k].name);
                }
        
                var arrTest = arr.slice(3);
        
                arr.splice(0,3);
                arrTest.splice(2, 0, "三", "四");
                arrTest.splice(2, 2, 3, 4);
                var arrTest2 = arrTest.concat(6, 7, 8);
                console.log(arrTest2);
                arrTest2.reverse();
                console.log(arrTest2);

                function compare(value1, value2){
                    return value1 - value2;
                }
                arrTest2.sort(compare);
                alert(arrTest2); */

        //批量定义变量（利用对象，定义为对象的属性）
                var nodes = {};
                for (let i = 1; i < 6; i++){
                    nodes['a'+ i] = i;
                }

                console.log(nodes[a1]);
        
/*                 for (k in nodes){
                    console.log(k + "=" + nodes[k]);
                } */


    </script>
</head>

<body>

</body>

</html>